ccR.Add_Rotation(
	1,
	"CC_Balance_Solo",
	"Created by: CrazyCactuaR\n\nFor Levelling/Dungeons."
)

function CC_Balance_Solo()
	-- Moonkin Form
	if ccR.Spell.Is.Ready(MOONKIN_FORM) and ccR.Player.Stance() ~= 4 then ccR.Cast.Spell(MOONKIN_FORM) end
	if ccR.Player.Stance() ~= 4 then return end
	-- Mark of the Wild
	if ccR.Spell.Is.Ready(MARK_OF_THE_WILD)
	and not ccR.Player.Has.Buff(MARK_OF_THE_WILD)
	and not ccR.Player.Has.Buff(BARK_OF_THE_WILD)
	and not ccR.Player.Has.Buff(BLESSING_OF_KONGS)
	and not ccR.Player.Has.Buff(EMBRACE_OF_THE_SHALE_SPIDER)
	and not ccR.Player.Has.Buff(STRENGTH_OF_THE_EARTH)
	and not ccR.Player.Has.Buff(LEGACY_OF_THE_WHITE_TIGER)
	and not ccR.Player.Has.Buff(LEGACY_OF_THE_EMPEROR) 
	and not ccR.Player.Has.Buff(BLESSING_OF_KINGS) 
	then
		ccR.Cast.Spell(MARK_OF_THE_WILD)	
	end
	-- Solar Beam
	if ccR.Spell.Is.Ready(SOLAR_BEAM) then 
		for x = 1, # ccR_Enemies_Within_44 do 
			local vObject = ccR_Enemies_Within_44[x] 
			if ccR.Unit.Exists(vObject) 
			and ccR.Unit.IsAlive(vObject)
			and ccR.Spell.CanAttack(SOLAR_BEAM, vObject) 
			and (ccR.Unit.IsTappedByPlayer(vObject) or ccR.Unit.TargetName(vObject) == ccR.Player.Name())
			and ccR.Unit.IsCastingOrChanneling(vObject)
			then
				if ccR.Player.Is.CastingOrChanneling() then SpellStopCasting() end 
				ccR.Cast.AF.Attack(SOLAR_BEAM, vObject)
			end
		end
	end	
	-- Self Healing
	if ccR.Spell.Is.Ready(RENEWAL) and ccR.Player.Health() <= 78 then ccR.Cast.Spell(RENEWAL) end
	if ccR.Spell.Is.Ready(REJUVENATION) and ccR.Player.Health() <= 90 and not ccR.Player.Has.Buff(REJUVENATION) then ccR.Cast.Spell(REJUVENATION, "player") end
	-- In Combat
	if ccR.Player.Is.InCombat() then
		if ccR.Spell.Is.Ready(CELESTIAL_ALIGNMENT) and (ccR.Player.Has.Buff(LUNAR_EMPOWERMENT) or ccR.Player.Has.Buff(SOLAR_EMPOWERMENT)) then ccR.Cast.Spell(CELESTIAL_ALIGNMENT) end
	end
	-- Melee enemies
	if ccR.Player.Enemies() > 0 and ccR.Player.Count(10) > 0 then
		if ccR.Spell.Is.Ready(BARKSKIN) then ccR.Cast.Spell(BARKSKIN) end
	end
	-- 3+
	if ccR.Player.Enemies() >= 3 then
		if ccR.Spell.Is.Ready(STARFALL) and not ccR.Player.Has.Buff(STARFALL) and ((ccR.DebuffCount(MOONFIRE) + ccR.DebuffCount(SUNFIRE)) >= ccR.Player.Enemies()) then ccR.Cast.Spell(STARFALL) end
	end
	-- 2+
	if ccR.Player.Enemies() >= 2 then
		if ccR.Spell.Is.Ready(SUNFIRE) and (ccR.Player.Is.Solar() or ccR.Player.Has.Buff(CELESTIAL_ALIGNMENT)) then ccR.Cast.AF.Mass.Debuff(SUNFIRE, "all", 40, true, 3) end
		if ccR.Spell.Is.Ready(MOONFIRE) and (ccR.Player.Is.Lunar() or ccR.Player.Has.Buff(CELESTIAL_ALIGNMENT)) then ccR.Cast.AF.Mass.Debuff(MOONFIRE, "all", 40, true, 3) end
		if ccR.Player.Has.Talent(MASS_ENTANGLEMENT) and ccR.Spell.CanAttack(MASS_ENTANGLEMENT) and ccR.Target.Count(12) > 3 then ccR.Cast.Attack(MASS_ENTANGLEMENT) end
	end
	-- Target
	if ccR.Target.Is.Attackable() then
		-- Moving
		if ccR.Player.Is.Moving() then
			if ccR.Spell.CanAttack(MOONFIRE) and ccR.Player.Is.Lunar() then ccR.Cast.AF.Attack(MOONFIRE) end
			if ccR.Spell.CanAttack(SUNFIRE) and ccR.Player.Is.Solar() then ccR.Cast.AF.Attack(SUNFIRE) end			
			if ccR.Spell.CanAttack(STARSURGE) 
			and ccR.Player.Has.Spell(ENHANCED_STARSURGE)
			and (
					ccR.Spell.Charges(STARSURGE) == 3
					or
					(
						(ccR.Player.Is.Lunar() and (not ccR.Player.Has.Buff(LUNAR_EMPOWERMENT) and ccR.Player.Eclipse() < -80))
						or
						(ccR.Player.Is.Solar() and (not ccR.Player.Has.Buff(SOLAR_EMPOWERMENT) and ccR.Player.Eclipse() > 80))
					)
				)
			and ccR.Player.Is.Facing("target")
			then
				ccR.Cast.Attack(STARSURGE)
			end
		else
		-- Stood still
			if ccR.Spell.CanAttack(MOONFIRE) and (ccR.Player.Is.Lunar() or ccR.Player.Has.Buff(CELESTIAL_ALIGNMENT)) and ((not ccR.Target.Has.MyDebuff(MOONFIRE) or ccR.Target.MyDebuffDuration(MOONFIRE, "<", 3)) or ccR.Player.Has.Buff(LUNAR_PEAK)) then ccR.Cast.AF.Attack(MOONFIRE) end
			if ccR.Spell.CanAttack(SUNFIRE) and (ccR.Player.Is.Solar() or ccR.Player.Has.Buff(CELESTIAL_ALIGNMENT)) and ((not ccR.Target.Has.MyDebuff(SUNFIRE) or ccR.Target.MyDebuffDuration(SUNFIRE, "<", 3)) or ccR.Player.Has.Buff(SOLAR_PEAK)) then ccR.Cast.AF.Attack(SUNFIRE) end			
			if ccR.Spell.CanAttack(STARSURGE) 
			and (
					ccR.Spell.Charges(STARSURGE) == 3
					or
					(
						(ccR.Player.Is.Lunar() and (not ccR.Player.Has.Buff(LUNAR_EMPOWERMENT) and ccR.Player.Eclipse() < -80))
						or
						(ccR.Player.Is.Solar() and (not ccR.Player.Has.Buff(SOLAR_EMPOWERMENT) and ccR.Player.Eclipse() > 80))
					)
				)
			and ccR.Player.Is.Facing("target")
			then
				ccR.Cast.Attack(STARSURGE)
			end
			if ccR.Spell.CanAttack(STARFIRE) and (ccR.Player.Has.Buff(LUNAR_EMPOWERMENT) or ccR.Player.Is.Lunar()) and ccR.Player.Is.Facing("target") then ccR.Cast.Attack(STARFIRE) end
			if ccR.Spell.CanAttack(WRATH) and (ccR.Player.Has.Buff(SOLAR_EMPOWERMENT) or ccR.Player.Is.Solar()) and ccR.Player.Is.Facing("target") then ccR.Cast.Attack(WRATH) end
		end
	end
end